package org.geektimes.projects.user.bean.validation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PhoneValidator implements ConstraintValidator<Phone, CharSequence> {
    private final Pattern pattern = java.util.regex.Pattern.compile("^1[34578]\\d{9}$");

    @Override
    public void initialize(Phone constraintAnnotation) {

    }

    @Override
    public boolean isValid(CharSequence value, ConstraintValidatorContext context) {
        if (value == null || value.length() == 0) {
            return true;
        }
        Matcher matcher = pattern.matcher(value);
        return matcher.matches();
    }
}
